package com.collect.zambia.zhiScene.zhiCollection.zhiBigGroup.zhiPerformance;

import android.content.Context;

import com.collect.zambia.zhiBase.BasePresenter;
import com.collect.zambia.zhiBase.BaseSubscriber;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupBonusEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupPerformanceMoneyEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRGroupUserPerformanceMergeEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISendEntity;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiNetApi.ZHIHttpApiProvider;
import com.collect.zambia.zhiNetApi.ZHIHttpResponse;
import com.collect.zambia.zhiScene.zhiCollection.zhiAdmin.ZHIAdminReduce;
import com.collect.zambia.zhiUtils.RxSchedulers;

import java.util.ArrayList;

import javax.inject.Inject;

public class ZHIPerformanceBigGroupPresenter extends BasePresenter<ZHIPerformanceBigGroupContract.View> implements ZHIPerformanceBigGroupContract.Presenter {

    @Inject
    public ZHIPerformanceBigGroupPresenter() {

    }

    @Override
    public void getGroupUserPerformanceOrderMerge(Context context, ZHISPerfermanceEntity zhisPerfermanceEntity, boolean showDialog) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupUserPerformanceMergeEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupUserPerformanceMergeEntity>>>(context, this, showDialog) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRGroupUserPerformanceMergeEntity> zhirGroupUserPerformanceMergeEntityArrayList = (ArrayList<ZHIRGroupUserPerformanceMergeEntity>) object;
                            mView.onGetGroupUserPerformanceOrderMerge(zhirGroupUserPerformanceMergeEntityArrayList, zhisPerfermanceEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().myGroupUserPerformanceOrderMerge(zhisPerfermanceEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getGroupUserPerformanceMoneyMerge(Context context, ZHISPerfermanceEntity zhisPerfermanceEntity, boolean showDialog) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupPerformanceMoneyEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupPerformanceMoneyEntity>>>(context, this, showDialog) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRGroupPerformanceMoneyEntity> zhirGroupPerformanceMoneyEntityArrayList = (ArrayList<ZHIRGroupPerformanceMoneyEntity>) object;
                            mView.onGetGroupUserPerformanceMoneyMerge(zhirGroupPerformanceMoneyEntityArrayList, zhisPerfermanceEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().myGroupUserPerformanceMoneyMergeBigGroup(zhisPerfermanceEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getGroupUsers(Context context, ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupUserEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupUserEntity>>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRGroupUserEntity> zhirGroupUserEntityArrayList = (ArrayList<ZHIRGroupUserEntity>) object;
                            if (zhirGroupUserEntityArrayList == null) {
                                zhirGroupUserEntityArrayList = new ArrayList<>();
                            }
                            ZHIAdminReduce.mGroupUserEntityArrayList = zhirGroupUserEntityArrayList;
                            ZHIMainProcessHelper.getInstance().setGroupUserEntityArrayList(zhirGroupUserEntityArrayList);
                            mView.onGetGroupUsers(zhirGroupUserEntityArrayList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().myGroupUsers(zhiSendEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getGroupBonus(Context context, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRGroupBonusEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRGroupBonusEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRGroupBonusEntity zhirGroupBonusEntity = (ZHIRGroupBonusEntity) object;
                            mView.onGetGroupBonus(zhirGroupBonusEntity, zhisPerfermanceEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().myGroupBonus(zhisPerfermanceEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getBigGroupUsers(Context context, boolean isFirstLoad, ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupUserEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRGroupUserEntity>>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRGroupUserEntity> bigGroupUserList = (ArrayList<ZHIRGroupUserEntity>) object;
                            if (bigGroupUserList == null) {
                                bigGroupUserList = new ArrayList<>();
                            }
                            mView.onGetBigGroupUsers(isFirstLoad, bigGroupUserList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().queryBigGroupUsers(zhiSendEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }
}
